package ws.utils.collections.table;

import java.util.LinkedList;
import java.util.List;
import ws.utils.collections.table.exception.ColumnNotFoundException;

/**
 * This class represents one table row.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class Row {

  /** Column list for this row. */
  protected List<Column> columnList = new LinkedList<Column>();

  /**
   * Append one column to this row.
   *
   * @param col column to be added to the end of column list.
   */
  public void addColumn(Column col) {
    columnList.add(col);
  }

  /**
   * Remove one column from this row.
   *
   * @param index index of the column to be removed.
   *
   * @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= columnCount())}.
   */
  public void removeColumn(int index) {
    columnList.remove(index);
  }

  /**
   * Get the reference to one column on this row.
   *
   * @param index column number (they are numbered from {@code 0 to columnCount()-1}).
   *
   * @return the column the at the specified position in this row.
   *
   * @throws IndexOutOfBoundsException if the index is out of range {@code (index < 0 || index >= columnCount())}.
   */
  public Column getColumn(int index) {
    return columnList.get(index);
  }

  /**
   * Get the contents of one column.
   *
   * @param columnName column name.
   *
   * @return contents of the column.
   *
   * @throws ColumnNotFoundException if the column does not exist on the table.
   */
  public Object getColumnContents(String columnName) throws ColumnNotFoundException {
    for (Column column : columnList) {
      if (column.getName().compareToIgnoreCase(columnName) == 0) {
        return column.content;
      }
    }
    throw new ColumnNotFoundException(columnName);
  }

  /**
   * Get the columns on this row.
   * 
   * @return list with the columns.
   */
  public List<Column> getColumnList() {
    return columnList;
  }

  /**
   * Get the number of columns of this row.
   * 
   * @return the number of columns of this row.
   */
  public int columnCount() {
    return columnList.size();
  }

  @Override
  public String toString() {
    String str = "";
    for (Column col : columnList) {
      str += col.toString();
      str += '\t';
    }
    str += '\n';
    return str;
  }
}
